package com.wondersgroup.wuhan_demo.ui;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.google.gson.Gson;
import com.wondersgroup.wuhan_demo.R;
import com.wondersgroup.wuhan_demo.base.BaseActivity;
import com.wondersgroup.wuhan_demo.entity.VoiceEntity;
import com.wondersgroup.wuhan_demo.util.FileUtil;
import com.wondersgroup.wuhan_demo.util.SPUtils;
import com.wondersgroup.wuhan_demo.widget.GuideTopWidget;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mrobot.libs.speech.nlu.model.bean.VCBusiness;

public class LaunchActivity extends BaseActivity {
    @BindView(R.id.gtw)
    GuideTopWidget gtw;
    private String TAG = this.getClass().getSimpleName();

    private FrameLayout content;
    private Button btnBack, btnMain;

    private VoiceEntity voiceEntity;
    private String normValue;

    private String tochTime;

    public static void start(Context context) {
        Intent starter = new Intent(context, LaunchActivity.class);
//        starter.putExtra();
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        ButterKnife.bind(this);
        btnBack = findViewById(R.id.btnBack);
        btnMain = findViewById(R.id.btnMain);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
                    getSupportFragmentManager().popBackStackImmediate();
                } else {
                    finish();
                }
                 setTopPosition(getSupportFragmentManager().getBackStackEntryCount() );

            }
        });
        btnMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int size=getSupportFragmentManager().getBackStackEntryCount();
                Log.i("position==",size+"");
                for (int i=1;i<size; i++) {
                    Log.i("i==",i+"");
                    getSupportFragmentManager().popBackStackImmediate();
                }
                Log.i("position==1",getSupportFragmentManager().getBackStackEntryCount()+"");
                setTopPosition(1);
            }
        });
        content = findViewById(R.id.content);

        sancerCard();
        getSupportFragmentManager().beginTransaction().add(content.getId(), ScanInfoFragment.newInstance(normValue)).addToBackStack(null).commit();
//        getSupportFragmentManager().beginTransaction().add(content.getId(),new DhFragment()).addToBackStack(null).commit();
    }

    private void sancerCard() {
        VCBusiness vcBusiness = (VCBusiness) getIntent().getSerializableExtra("extra_business");
        if (vcBusiness != null) {
            voiceEntity = new Gson().fromJson(vcBusiness.getRawBusinessString(), VoiceEntity.class);
            Log.i("vcBusiness==", vcBusiness.getRawBusinessString() + "");
            normValue = voiceEntity.getSemantic().get(0).getSlots().get(0).getNormValue();
            Log.i("normValue", normValue + "");
        }
    }

    @Override
    public void onBackPressed() {
        // 这里做返回键的处理
        super.onBackPressed();
        Log.i("onBackPressed==", getSupportFragmentManager().getBackStackEntryCount() + "");
        if (getSupportFragmentManager().getBackStackEntryCount() <= 1) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Resources resources =getResources();
        SPUtils.put(getApplicationContext(),resources.getString(R.string.kh),"");
        SPUtils.put(getApplicationContext(),resources.getString(R.string.xm),"");
        SPUtils.put(getApplicationContext(),resources.getString(R.string.xb),"");
        if (handler!=null){
            handler.removeCallbacksAndMessages(null);
        }

    }

    public  void setTopPosition(int position) {
        if (position>0&&position<=4) {
            gtw.setVisibility(View.VISIBLE);
            gtw.setGuidePosition(position);
            content.setBackgroundResource(R.color.gray_0_5a);

        }else {
            content.setBackground(null);
            gtw.setVisibility(View.GONE);
        }
    }



}
